package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwglT;
import cn.e4j.modular.crm.mapper.GwglTMapper;
import cn.e4j.modular.crm.service.GwglTService;
import cn.e4j.modular.oa.service.OAAbandonWsService;
import cn.e4j.modular.oa.ws.common.SoapService;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuwei
 * @since 2019-12-05
 */
@Service
public class GwglTServiceImpl extends ServiceImpl<GwglTMapper, GwglT> implements GwglTService {

    @Autowired
    SysOrgMapper sysOrgMapper;

    @Override
    public List<Dto> getHeadList(Page<Object> page, Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        return InvokeUtil.invokeBeanMethod("gwglTMapper,getHeadList", page, inDto);
    }

    @Override
    public void add(GwglT gwglT) {
        QueryWrapper<GwglT> gwglTQueryWrapper = new QueryWrapper<>();
        gwglTQueryWrapper.eq("GWGLENT", gwglT.getGWGLENT());
        int count = this.baseMapper.selectCount(gwglTQueryWrapper) + 1;
        String num = String.format("%04d", count);
        gwglT.setGWGLID(DateUtils.getDate("yyMMdd") + num);
        gwglT.insert();
    }

    @Override
    public void edit(GwglT gwglT) {
        UpdateWrapper<GwglT> gwglTUpdateWrapper = new UpdateWrapper<>();
        gwglTUpdateWrapper.eq("GWGLENT", gwglT.getGWGLENT());
        gwglTUpdateWrapper.eq("GWGLID", gwglT.getGWGLID());
        gwglT.update(gwglTUpdateWrapper);
    }

    @Override
    public void delete(GwglT gwglT) {
        UpdateWrapper<GwglT> gwglTUpdateWrapper = new UpdateWrapper<>();
        gwglTUpdateWrapper.eq("GWGLENT", gwglT.getGWGLENT());
        gwglTUpdateWrapper.eq("GWGLID", gwglT.getGWGLID());
        new GwglT().delete(gwglTUpdateWrapper);
    }

    @Override
    public void deleteBatch(List<GwglT> gwglTList) {
        gwglTList.forEach(this::delete);
    }

    @Override
    public Dto changeStatusBatch(List<GwglT> gwglTList) {
        Dto outDto = Dtos.newDto();
        String state = gwglTList.get(0).getGWGLACTI();
        for (GwglT gwglT : gwglTList) {
            UpdateWrapper<GwglT> gwglTUpdateWrapper = new UpdateWrapper<>();
            gwglTUpdateWrapper.eq("GWGLENT", gwglT.getGWGLENT());
            gwglTUpdateWrapper.eq("GWGLID", gwglT.getGWGLID());
            gwglT.update(gwglTUpdateWrapper);
        }
        outDto.setAppCode(200);
        return outDto;
    }

    @Override
    public List<Dto> getBookmarkData(Page page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwglTMapper,getBookmarkData", page, inDto);
    }

    @Override
    public Dto getGwgc014(Dto inDto) {
        return this.baseMapper.getGwgc014(inDto);
    }

}
